package com.hdyg.sixg.activity.mine;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.MyTeamAdapter;
import com.hdyg.sixg.entry.MyTeamBean;
import com.hdyg.sixg.mvp.Contrant.CMyTeam;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PMyTeam;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.hdyg.sixg.util.AlertDialogUtil;
import com.hdyg.sixg.util.DataCenter;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author CZB
 * @describe 我的团队
 * @time 2019/4/8 16:04
 */
public class MyTeamActivity extends BaseActivity implements CMyTeam.IVMyTeam {
    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private HeadViewHolder headViewHolder;
    private View headView;
    private MyTeamAdapter mAdapter;
    private CMyTeam.IPMyTeam mPresenter;
    private int page = 1,allDataSize;
    private List<MyTeamBean.TeamlistBean> teamList;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_recyclerview;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.team_title);
        rvMain.setLayoutManager(new LinearLayoutManager(mContext));
        headView = StringUtil.getView(R.layout.item_team_top,mContext);
        headViewHolder = new HeadViewHolder(headView);
    }
    private void onClickListener(){
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.MY_TEAM_URL,GetParamUtil.getMyTeamParam(page));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PMyTeam(this);
    }

    private void initAdapter(){
        mAdapter = new MyTeamAdapter(R.layout.item_team,teamList);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.addHeaderView(headView);
        rvMain.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvMain);
        headViewHolder.btApply.setOnClickListener(v -> {
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            AlertDialogUtil.getInstance().showDialog(mContext, R.string.sys_toast, R.string.sure_apply, () ->
                mPresenter.pGetApplyHolder(BaseUrl.APPLY_HOLDER_URL,GetParamUtil.getApplyHolderParam()),() -> {

            }, true);
        });
    }

    @Override
    public void vGetInitSuccess(MyTeamBean dataBean) {

        teamList = dataBean.getTeamlist();
        allDataSize = dataBean.getNum();
        headViewHolder.svAllPerformance.setCenterTopString(dataBean.getAll_yj());   //团队总成绩
        headViewHolder.svAllPerson.setCenterTopString(dataBean.getTeam_all_num());  //团队总人数
        headViewHolder.svPartnerPerformance.setCenterTopString(dataBean.getPartner_yj());//合伙人业绩
        headViewHolder.svMaxAddPerformance.setCenterTopString(dataBean.getBig_add_yj());    //大区新增业绩/天
        headViewHolder.svMaxAllPerformance.setCenterTopString(dataBean.getBig_yj());      //大区累计业绩
        headViewHolder.svMinAddPerformance.setCenterTopString(dataBean.getSmall_add_yj());  //小区新增业绩/天
        headViewHolder.svMinAllPerformance.setCenterTopString(dataBean.getSmall_yj());        //小区累计业绩
        if (teamList.size() == 0){
            headViewHolder.llTeamPerson.setVisibility(View.GONE);
        }else {
            headViewHolder.llTeamPerson.setVisibility(View.VISIBLE);
        }
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(teamList);
            } else {
                initAdapter();
            }
        } else {
            if (teamList.size() != 0) {
                mAdapter.addData(teamList);
            }
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void vGetApplyHolderSuccess(String dataBean) {
        toastNotifyShort(dataBean);
    }

    static class HeadViewHolder {
        @BindView(R.id.sv_all_performance)
        SuperTextView svAllPerformance;
        @BindView(R.id.sv_all_person)
        SuperTextView svAllPerson;
        @BindView(R.id.sv_partner_performance)
        SuperTextView svPartnerPerformance;
        @BindView(R.id.sv_max_add_performance)
        SuperTextView svMaxAddPerformance;
        @BindView(R.id.sv_max_all_performance)
        SuperTextView svMaxAllPerformance;
        @BindView(R.id.sv_min_add_performance)
        SuperTextView svMinAddPerformance;
        @BindView(R.id.sv_min_all_performance)
        SuperTextView svMinAllPerformance;
        @BindView(R.id.bt_apply)
        Button btApply;
        @BindView(R.id.ll_team_person)
        LinearLayout llTeamPerson;

        HeadViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
